package com.example.homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get      >>>");
        String paramsStr = req.getQueryString();
        String[] paramsArr = paramsStr.split("&");

        String username = paramsArr[0].split("=")[1];
        String password = paramsArr[1].split("=")[1];

        if ("admin".equals(username) && "admin".equals(password)) {
            System.out.println("登录成功！");
        } else {
            System.out.println("登录失败!");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post     >>>");
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            String[] paramsArr = line.split("&");

            String username = paramsArr[0].split("=")[1];
            String password = paramsArr[1].split("=")[1];

            if ("admin".equals(username) && "admin".equals(password)) {
                System.out.println("登录成功！");
            } else {
                System.out.println("登录失败!");
            }
        }
    }
}